import wx

class DiaChangeBookName(wx.Dialog):
    def __init__(self, parent, title, *args, **kwargs):
        super().__init__(parent, -1, title)
        self.Names = kwargs.get('names')
        self.Value = kwargs.get('value', '')
        TextCtrl = wx.TextCtrl(self, -1, name='TextCtrl', size=(400, 25), value=self.Value)
        BtnSave = wx.Button(self, -1, '保存', name='Save')
        BtnCancel = wx.Button(self, -1, '取消', name='Cancel')
        self.Bind(wx.EVT_BUTTON, self.OnSave, BtnSave)
        self.Bind(wx.EVT_BUTTON, self.OnCancel, BtnCancel)

        MainSizer = wx.BoxSizer(orient=wx.VERTICAL)
        BtnSizer = wx.BoxSizer(orient=wx.HORIZONTAL)

        MainSizer.Add(TextCtrl, 0, wx.EXPAND|wx.ALL, 5)
        MainSizer.Add(wx.StaticLine(self), 0,
                wx.EXPAND|wx.TOP|wx.BOTTOM, 0)
        MainSizer.Add(BtnSizer, 0, wx.ALL|wx.ALIGN_RIGHT, 5)
        BtnSizer.Add(BtnSave)
        BtnSizer.Add(BtnCancel)

        self.SetSizer(MainSizer)
        MainSizer.Fit(self)
        MainSizer.SetSizeHints(self)

    def OnCancel(self, event):
        self.EndModal(retCode=5101)

    def ShowModal(self):
        ret = super().ShowModal()
        if ret == 5101:
            return ret
        elif ret == 1:
            obj = self.FindWindowByName('TextCtrl')
            ret = obj.GetLineText(0)
            return ret
        else:
            return 5101

    def OnSave(self, event):
        obj = self.FindWindowByName('TextCtrl')
        ret = obj.GetLineText(0)
        if ret in self.Names:
            wx.MessageBox('分册名称不能重复!'
                , '警告!',style=wx.OK|wx.ICON_WARNING)
            return
        elif ret == '':
            wx.MessageBox('分册名称不能为空!'
                , '警告!',style=wx.OK|wx.ICON_WARNING)
            return

        self.EndModal(retCode=1)

    

if __name__ == "__main__":


    class MyFrame(wx.Frame):
        def __init__(self):
            super().__init__(None, -1, 'haha', size=(200, 100))
            btn = wx.Button(self, -1, 'baba', size=(50, 50), pos=(0,0))
            btn2 = wx.Button(self, -1, 'baba', size=(50, 50), pos=(50,0))
            self.Bind(wx.EVT_BUTTON, self.OnClick, btn)
            self.Bind(wx.EVT_BUTTON, self.OnClick2, btn2)

        def OnClick(self, event):
            # modal = DiaTable2add(self, 'baba').ShowModal()
            Names = ['1', '2', '3', '4', '5', '6', ]
            Value = 'mmp'
            dia = DiaChangeBookName(self, 'haha', names=Names, value=Value)
            modal = dia.ShowModal()
            print(modal)
            dia.Destroy()

        def OnClick2(self, event):
            # modal = DiaTable2add(self, 'baba').ShowModal()
            Names = ['1', '2', '3', '4', '5', '6', ]
            dia = wx.TextEntryDialog(self, 'haha')
            modal = dia.ShowModal()
            print(modal)
            dia.Destroy()

    app = wx.App()
    MyFrame().Show()
    app.MainLoop()